﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Office365.SharePointOnline.Cmdlets
{
    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Set, "MasterPage")]
    public class Set_MasterPage : System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        public string Url;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = true)]
        public string MasterPageUrl;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = true)]
        public string SystemMasterUrl;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = false)]
        public string User;

        [System.Management.Automation.Parameter(Position = 4, Mandatory = false)]
        public string Password;

        [System.Management.Automation.Parameter(Position = 5, Mandatory = false)]
        public string AuthenticationMethod;

        // use only if onpremise authentication
        [System.Management.Automation.Parameter(Position = 6, Mandatory = false)]
        public string Domain;


        protected override void ProcessRecord()
        {
            // check parameters
            ClientContext context = new ClientContext(Url);
            Web web = context.Web;
            ICredentials credentials;
            if (string.IsNullOrEmpty(Url))
            {
                this.WriteObject("Error: url is not set");
            }
            else
            {
                if (!string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(User))
                {
                    AuthenticationHelper authenticationHelper = new AuthenticationHelper() { AuthenticationMethod = AuthenticationMethod, Domain = Domain, Password = Password, User = User };
                    credentials = authenticationHelper.GetCredentials();
                    if (credentials != null)
                    {
                        context.Credentials = credentials as ICredentials;
                    }
                    else
                    {
                        throw new NullReferenceException("No credentials returned.");
                    }
                }
                else
                {
                    this.WriteVerbose("Reusing existing credentials");
                }
                SetMasterPage(context, web);
            }

        }

        private void SetMasterPage(ClientContext context, Web web)
        {
            this.WriteVerbose("Setting masterpage.");
            context.Load(web);
            context.ExecuteQuery();

            web.MasterUrl = MasterPageUrl;
            web.CustomMasterUrl = MasterPageUrl;
            web.Update();
            context.ExecuteQuery();
            this.WriteObject(string.Format("Finished setting master url to: {0} and system master to: {1} for site: {2}", MasterPageUrl, SystemMasterUrl, Url));
        }
    }
}
